<?php

class AuthController extends Zend_Controller_Action
{
    public function init ()
    {
       
        $layout = Zend_Layout::getMvcInstance();
        $layout->disableLayout();
        
    }
    public function indexAction ()
    {
        
        $auth = Zend_Auth::getInstance();
       
        $auth->setStorage(new Zend_Auth_Storage_Session('Zend_Auth_CMDB')); 
        if ($auth->hasIdentity())
        {
            $this->_helper->redirector('search', 'equipment');
        } 
//        $ss = new Zend_Session_Namespace('referer');
//        $request = $this->getRequest();
//        if($request->getHeader('referer')){
//			$ss->referer = $request->getHeader('referer');
//            echo $ss->referer;			
//        }
        $form_login = new Default_Form_Auth();
        if ($this->_request->isPost())
        {
            $form_data = $this->_request->getPost();
            if ($form_login->isValid($form_data))
            {   
               
                $adapter = new Zend_Auth_Adapter_DbTable(NULL,'users','username','password','MD5(?)');
                
                $adapter->setIdentity($form_login->getValue('username'));
             
                $adapter->setCredential($form_login->getValue('password'));
                  
                $result = $auth->authenticate($adapter);
             
                if ($result->isValid())
                {         
                    
					$model_users = new Default_Model_Users();
                    $user_check = $model_users->getUserByUsername($form_login->getValue('username'));
                  
                    if($user_check->status == 1){
                        $auth->getStorage()->write(array('username' => $user_check->username, 'userId' => $user_check->userId));
//                        if(isset($ss->referer)){
//                            echo $ss->referer;exit;
//                        }
                        return $this->_helper->redirector('search', 'equipment');
                    }else{
                        $auth->clearIdentity();
                        session_destroy();
                        $this->view->mess = 'Tài khoản của bạn đang tạm khóa!';
                        $this->view->form = $form_login;
                    }
                }else{
                    $this->view->mess = 'Username hoặc mật khẩu không đúng!';
                    $this->view->form = $form_login;
                }
            }else{
                $this->view->mess = 'Vui lòng kiểm tra lại thông tin đăng nhập!';
                $this->view->form = $form_login;
            }
        } else {
            $this->view->form = $form_login;
        }
    }
    public function logoutAction ()
    {
        $auth = Zend_Auth::getInstance();
        $auth->setStorage(new Zend_Auth_Storage_Session('Zend_Auth_CMDB'));
        $auth->clearIdentity();
        return $this->_helper->redirector('index','index','default');
    }
}



